package com.ymatou.apigateway.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.BillCategoryInfo;
import com.ymatou.apigateway.parameter.BillReceiverInfo;
import com.ymatou.apigateway.parameter.BillSenderInfo;
import com.ymatou.apigateway.parameter.BillSupplyInfo;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.OpenapiService;
import com.ymatou.apigateway.parameter.xlobocreateNoVerificationBean;
import com.ymatou.apigateway.service.xlobocreateNoVerificationCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*贝海做单
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_xlobocreateNoVerification{
	private static OpenapiReq openapiReq;

	private static xlobocreateNoVerificationBean req;
	private static xlobocreateNoVerificationCall xlobocreatenoverificationCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海做单");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("xlobo.labels.create");

		req=new xlobocreateNoVerificationBean();
		xlobocreatenoverificationCall=new xlobocreateNoVerificationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(xlobocreatenoverificationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xlobocreateNoVerification_001() {
		Logger.start(true,"贝海做单");
		try {
			  	req.setBusiness_no("1");
		        req.setWeight(new BigDecimal("11"));
		        req.setInsure(new BigDecimal("100"));
		        req.setComment("test");
		        req.setLogistic_id(1);
		        req.setLogistic_version("1.0");
		        req.setLine_type_id(1);
		        req.setIs_pre_tax(1);
		        req.setIs_rec_tax(1);
		        req.setIs_repacking(1);
		        req.setIs_contain_tax(0);
		        
		        BillSenderInfo billSenderInfo = new BillSenderInfo();
		        billSenderInfo.setName("点点点");
		        billSenderInfo.setAddress("的说法啊沙发发");
		        billSenderInfo.setPhone("17216214569878");
		        req.setBill_sender_info(billSenderInfo);

		        BillReceiverInfo billReceiverInfo = new BillReceiverInfo();
		        billReceiverInfo.setName("zhangsan");
		        billReceiverInfo.setProvince("湖南");
		        billReceiverInfo.setCity("长沙");
		        billReceiverInfo.setDistrict("湘潭");
		        billReceiverInfo.setAddress("jkhdsjkhdsfjkhds");
		        billReceiverInfo.setPhone("13126682379");
		        billReceiverInfo.setPost_code("100000");

		        req.setBill_receiver_info(billReceiverInfo);

		        BillSupplyInfo billSupplyInfo = new BillSupplyInfo();
		        billSupplyInfo.setChannel_name("洋码头");
		        billSupplyInfo.setTrading_no("14172801");
		        billSupplyInfo.setOrder_code("112524641");
		        req.setBill_supply_info(billSupplyInfo);

		        BillCategoryInfo billCategoryInfo = new BillCategoryInfo();
		        billCategoryInfo.setBrand("1");
		        //billCategoryInfo.setCategoryId("68b0d48a-3985-45fa-8b70-75713a126809");
		        billCategoryInfo.setCategory_id(15);
		        billCategoryInfo.setCategory_version("1.0");
		        billCategoryInfo.setCount(1);
		        billCategoryInfo.setProduct_name("【kstore2.28】无规格1");
		        billCategoryInfo.setUnit_price(new BigDecimal("100"));
		        billCategoryInfo.setModel("ss");
		        billCategoryInfo.setSpecification("1");
		        billCategoryInfo.setSize("1");
		        
		        List<BillCategoryInfo> billCategoryInfoes = new ArrayList<>();
		        billCategoryInfoes.add(billCategoryInfo);
//		        req.getBillCategoryList().add(billCategoryInfo);
		        req.setBill_category_list(billCategoryInfoes);
		        
		    	long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
//		        long sellerId = 20354944;
				OpenApiWapper openApiWapper = new OpenApiWapper();
				
				Application application = openApiWapper
						.selectApplicationBySellerId(sellerId).get(0);

				String app_id = application.getApp_id();
				String app_secret = application.getApp_secret();

				AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
						.get(0);

				openapiReq.setApp_id(app_id);
				openapiReq.setSign_method("MD5");
				openapiReq.setAuth_code(authCode.getAuth_code());
				openapiReq.setTimestamp(YMTDateUtil.getSysTime());
				openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
				openapiReq.setBiz_content(new FormatUtil()
						.beanToGSONString(req));
				openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
						AesUtil.decrypt(app_secret)));
			
				String url = String
						.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.labels.create",
								app_id);

				xlobocreatenoverificationCall.setUrl(url);	
				xlobocreatenoverificationCall.setData(openapiReq);
				xlobocreatenoverificationCall.callService();
				
				Logger.verifyEquals(true, xlobocreatenoverificationCall.getReturnData().contains("\"succeed\":true"), "验证数据库BusIp");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xlobocreateNoVerification_002() {
		Logger.start(true,"贝海做单-stg");
		try {
			  	req.setBusiness_no("1");
		        req.setWeight(new BigDecimal("11"));
		        req.setInsure(new BigDecimal("100"));
		        req.setComment("test");
		        req.setLogistic_id(1);
		        req.setLogistic_version("1.0");
		        req.setLine_type_id(1);
		        req.setIs_pre_tax(1);
		        req.setIs_rec_tax(1);
		        req.setIs_repacking(1);
		        req.setIs_contain_tax(0);
		        
		        BillSenderInfo billSenderInfo = new BillSenderInfo();
		        billSenderInfo.setName("点点点");
		        billSenderInfo.setAddress("的说法啊沙发发");
		        billSenderInfo.setPhone("17216214569878");
		        req.setBill_sender_info(billSenderInfo);

		        BillReceiverInfo billReceiverInfo = new BillReceiverInfo();
		        billReceiverInfo.setName("zhangsan");
		        billReceiverInfo.setProvince("湖南");
		        billReceiverInfo.setCity("长沙");
		        billReceiverInfo.setDistrict("湘潭");
		        billReceiverInfo.setAddress("jkhdsjkhdsfjkhds");
		        billReceiverInfo.setPhone("13126682379");
		        billReceiverInfo.setPost_code("100000");

		        req.setBill_receiver_info(billReceiverInfo);

		        BillSupplyInfo billSupplyInfo = new BillSupplyInfo();
		        billSupplyInfo.setChannel_name("洋码头");
		        billSupplyInfo.setTrading_no("14172801");
		        billSupplyInfo.setOrder_code("112524641");
		        req.setBill_supply_info(billSupplyInfo);

		        BillCategoryInfo billCategoryInfo = new BillCategoryInfo();
		        billCategoryInfo.setBrand("1");
		        //billCategoryInfo.setCategoryId("68b0d48a-3985-45fa-8b70-75713a126809");
		        billCategoryInfo.setCategory_id(15);
		        billCategoryInfo.setCategory_version("1.0");
		        billCategoryInfo.setCount(1);
		        billCategoryInfo.setProduct_name("【kstore2.28】无规格1");
		        billCategoryInfo.setUnit_price(new BigDecimal("100"));
		        billCategoryInfo.setModel("ss");
		        billCategoryInfo.setSpecification("1");
		        billCategoryInfo.setSize("1");
		        
		        List<BillCategoryInfo> billCategoryInfoes = new ArrayList<>();
		        billCategoryInfoes.add(billCategoryInfo);
//		        req.getBillCategoryList().add(billCategoryInfo);
		        req.setBill_category_list(billCategoryInfoes);
		        
		    	long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
//		        long sellerId = 20354944;

				String app_id = "6QcDcCtvV3pJknJUEp";
				String app_secret = "95605832511B6C85DB2D0FA9E3ACBB61FE3E1730322EF9CCA09E7ED5E523721461BEC16080D01798E84218559F4A6177";
				String auth_code = "2teKsa6gaiSUwxsACry9hXIaVsTEXqqV";

				openapiReq.setApp_id(app_id);
				openapiReq.setSign_method("MD5");
				openapiReq.setAuth_code(auth_code);
				openapiReq.setTimestamp(YMTDateUtil.getSysTime());
				openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
				openapiReq.setBiz_content(new FormatUtil()
						.beanToGSONString(req));
				openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
						AesUtil.decrypt(app_secret)));
			
				String url = String
						.format("https://open.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.labels.create",
								app_id);

				xlobocreatenoverificationCall.setUrl(url);	
				xlobocreatenoverificationCall.setData(openapiReq);
				xlobocreatenoverificationCall.callService();
				
				Logger.verifyEquals(true, xlobocreatenoverificationCall.getReturnData().contains("\"succeed\":true"), "验证数据库BusIp");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}